
package org.icnmasjid.member.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.icnmasjid.member.service.impl.BaseManager;
import org.icnmasjid.member.model.Address;
import org.icnmasjid.member.model.MemberInformation;
import org.icnmasjid.member.model.Person;
import org.icnmasjid.member.model.Role;
import org.icnmasjid.member.model.SubOrg;
import org.icnmasjid.member.dao.PersonDao;
import org.icnmasjid.member.service.PersonManager;

public class PersonManagerImpl extends BaseManager implements PersonManager {
    private PersonDao dao;

    /**
     * Set the Dao for communication with the data layer.
     * @param dao
     */
    public void setPersonDao(PersonDao dao) {
        this.dao = dao;
        setDao(dao);
    }

    /**
     * @see org.icnmasjid.member.service.PersonManager#getPersons(org.icnmasjid.member.model.Person)
     */
    public List getPersons(final Person person) {
        return dao.getPersons(person);
    }

    /**
     * @see org.icnmasjid.member.service.PersonManager#getPerson(String personId)
     */
    public Person getPerson(final String personId) {
        Person person = dao.getPerson(new Integer(personId));
        if ( person.getAddresses() != null && person.getAddresses().size() > 0) {
        	person.getAddressList().addAll(person.getAddresses());
        }
        person.getMemberYear().clear();
        for ( MemberInformation mi: person.getMemberInformations() ) {
        	person.getMemberYear().add(mi.getYearOfMembership());
        }
        return person;
    }

    /**
     * @see org.icnmasjid.member.service.PersonManager#savePerson(Person person)
     */
    public void savePerson(Person person) {
    	if (person.getCreatedDate() == null)
    		person.setCreatedDate(new Timestamp(new Date().getTime()));
    	for (Address a: person.getAddressList() ) {
    		// only add new address to this list
    		// if it is an existing address then it will update automatically
    		if ( a.getId() == null ) {
    			person.getAddresses().add(a);
    		}
    	}
        dao.savePerson(person);
    }

    /**
     * @see org.icnmasjid.member.service.PersonManager#removePerson(String personId)
     */
    public void removePerson(final String personId) {
        dao.removePerson(new Integer(personId));
    }

    public List getPersonsbyExample(final Person person) {
    	return dao.getPersonsbyExample(person);
    }

    public Person getPersonByEmail(String email) {
    	return dao.getPersonByEmail(email);
    }

    public List<Person> getPersons(Person person, String sortCriteria, String direction, Integer pageNumber, Integer numberOfResultsPerPage) {
    	return dao.getPersons(person, sortCriteria, direction, pageNumber, numberOfResultsPerPage);
    }

    public int getPersonsCount(Person person) {
    	return dao.getPersonsCount(person);
    }

    public List<Person> getPersonsWithEmail() {
    	return dao.getPersonsWithEmail();
    }
    
    public List<Person> getParentsWithEmail(Role schoolYearRole) {
    	return dao.getParentsWithEmail(schoolYearRole);
    }
    
    public List<Person> getParents(SubOrg grade, Role year) {
    	return dao.getParents(grade, year);
    }
    
    @Override
    public List<Person> getPersons(List<String> emails, List<String> numbers) {
    	return dao.getPersons(emails, numbers);
    }
    
    @Override
    public Long getGradeCount(String grade1, String roleName) {
    	return dao.getGradeCount(grade1, roleName);
    }
    
    public List<Person> getPrimaryPersonsWithEmail() {
    	return dao.getPrimaryPersonsWithEmail();
    }
    
    @Override
    public Person getLoadedPerson(Integer personId) {
    	return dao.getLoadedPerson(personId);
    }
}